﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication5.Controller;
using WebApplication5.Model;

namespace WebApplication5.Pages
{
    public partial class userstories : System.Web.UI.Page
    {
        protected userstoriesCtr uCtr = new userstoriesCtr();
        protected projectCtr pCtr = new projectCtr();
        protected void Page_Load(object sender, EventArgs e)
        {
            Site1 s = Page.Master as Site1;
            s.SelectedMenu = "userstories";

            if(!IsPostBack)
            {
            refresh();
            }
                //            List<Model.Userstories> ua = uCtr.GetAllUserstories();
                //var datasource = from u in ua
                //                 select new
                //                 {
                //                     u.actual,
                //                     u.asA,
                //                     u.id,
                //                     u.iWantTo,
                //                     u.priority,
                //                     u.Project,
                //                     u.projectId,
                //                     u.soThat,
                //                     u.Task,
                //                     u.name,
                //                     DisplayField = String.Format("{0} ({1})", u.name, u.id)
                //                 };

                //DropDownListEdit.DataSource = datasource;
                //DropDownListEdit.DataValueField = "id";
                //DropDownListEdit.DataTextField = "DisplayField";
                //DropDownListEdit.DataBind();


                //DropDownListDelete.DataSource = datasource;
                //DropDownListDelete.DataValueField = "id";
                //DropDownListDelete.DataTextField = "DisplayField";
                //DropDownListDelete.DataBind();
            
        }

        protected void refresh()
        {
            List<Model.Userstory> us = uCtr.GetAllUserstories();
            var datasource = from u in us
                             select new
                             {
                                 u.actual,
                                 u.asA,
                                 u.Estimate,
                                 u.id,
                                 u.iWantTo,
                                 u.name,
                                 u.priority,
                                 u.Project,
                                 u.projectId,
                                 u.soThat,
                                 u.Task,
                                 DisplayField = String.Format("{0} ({1})", u.name, u.id)
                             };

            DropDownListEdit.DataSource = datasource;
            DropDownListEdit.DataValueField = "id";
            DropDownListEdit.DataTextField = "DisplayField";
            DropDownListEdit.DataBind();


            DropDownListDelete.DataSource = datasource;
            DropDownListDelete.DataValueField = "id";
            DropDownListDelete.DataTextField = "DisplayField";
            DropDownListDelete.DataBind();

            List<Model.Project> ps = pCtr.GetAllProject();
            var datasourceProject = from p in ps
                             select new
                             {
                                p.cost,
                                p.endDate,
                                p.id,
                                p.Login,
                                p.loginId,
                                p.name,
                                p.startDate,
                                p.status,
                                p.time,
                                p.Userstory,


                                 DisplayField = String.Format("{0} ({1})", p.name, p.status)
                             };

            DropDownListAddProject.DataSource = datasourceProject;
            DropDownListAddProject.DataValueField = "id";
            DropDownListAddProject.DataTextField = "DisplayField";
            DropDownListAddProject.DataBind();


            DropDownEditProject.DataSource = datasourceProject;
            DropDownEditProject.DataValueField = "id";
            DropDownEditProject.DataTextField = "DisplayField";
            DropDownEditProject.DataBind();
        }


        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                Userstory u = new Userstory();
                u.id = int.Parse(DropDownListEdit.SelectedValue);
                u.actual = int.Parse(actualTbe.Text);
                u.asA = asATbe.Text;
                u.Estimate = int.Parse(estimateTbe.Text);
                u.iWantTo = iWantToTbe.Text;
                u.name = nameTbe.Text;
                u.priority = int.Parse(priorityTbe.Text);
                u.Project = null;
                u.projectId = int.Parse(DropDownEditProject.SelectedValue);
                u.soThat = soThatTbe.Text;
                u.Task = null;

                uCtr.editUserstory(u);

                Response.Redirect(Request.RawUrl);

                refresh();

              




            }


            catch (Exception ex)
            {
                Shared.alert(ex.Message, this);
            }

            Shared.reloadCurrentTab(this);
            Response.Redirect(Request.RawUrl);

        }




        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            int id = int.Parse(DropDownListDelete.SelectedValue);

            uCtr.deleteUserstory(id);

            Shared.reloadCurrentTab(this);

            refresh();
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {

                Userstory u = new Userstory();

                u.actual = int.Parse(actualTb.Text);
                u.asA = asAtb.Text;
                u.Estimate = int.Parse(estimateTb.Text);
                u.iWantTo = iWantToTb.Text;
                u.name = nameTb.Text;
                u.priority = int.Parse(priorityTb.Text);
                u.Project = null;
                u.projectId = int.Parse(DropDownListAddProject.SelectedValue);
                u.soThat = soThatTb.Text;
                u.Task = null;

                uCtr.AddUserstory(u);

            
            actualTb.Text = "";
            asAtb.Text = "";
            nameTb.Text = "";
            estimateTb.Text = "";
            iWantToTb.Text = "";
            priorityTb.Text = "";
            soThatTb.Text = "";

            refresh();

        }

        protected void ButtonGetUserstory_Click(object sender, EventArgs e)
        {


            int id = int.Parse(DropDownListEdit.SelectedValue);

            var U = uCtr.GetUserstory(id);

            
            actualTbe.Text = U.actual.ToString();
            asATbe.Text = U.asA;
            estimateTbe.Text = U.Estimate.ToString();
            iWantToTbe.Text = U.iWantTo;
            nameTbe.Text = U.name;
            priorityTbe.Text = U.priority.ToString();
            soThatTbe.Text = U.soThat;


        }
    
        
    }
        
    
}